/**
 * 
 */
package com.swg.cocobox.message;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Class sederhana sebagai container atau penampung dari {@link ClientInfo}. Mengimplementasikan {@link Iterable} supaya bisa foreach
 * @author satriaprayoga
 *
 */
public class ClientInfoList implements Serializable,Iterable<ClientInfo> {

	private static final long serialVersionUID = -520874644308314817L;
	
	private Set<ClientInfo> clientInfos;
	
	public ClientInfoList() {
		this.clientInfos=new HashSet<ClientInfo>();
	}
	
	public void addClientInfo(ClientInfo clientInfo){
		clientInfos.add(clientInfo);
	}
	
	public void removeClientInfo(ClientInfo clientInfo){
		clientInfos.remove(clientInfo);
	}

	@Override
	public Iterator<ClientInfo> iterator() {
		return clientInfos.iterator();
	}
	
	public boolean isEmpty(){
		return clientInfos.isEmpty();
	}
	
	/**
	 * Cari {@link ClientInfo} yang ditampung dalam object ini berdasarkan username
	 * @param username
	 * @return
	 */
	public ClientInfo getByUsername(String username){
		ClientInfo info=null;
		synchronized (clientInfos) {
			for(ClientInfo clientInfo:clientInfos){
				if(clientInfo.getUsername().equals(username)){
					info=clientInfo;
					break;
				}
				
			}
		}
		return info;
	}
	
	public int count(){
		return clientInfos.size();
	}

}
